Load imager
plant<- load.image('~/Documents/Github/PhD//imageprocessing/burseragrav1.jpg')
dim(plant)
## [1] 1400 2100 1 3
plot(plant)
## Y-axis shift
imshift(plant,0,50) %>% plot
## X-Axis
imshift(plant,50,0) %>% plot
imshift(plant,50,50) %>% plot
map_il(2:4,~ imresize(plant,1/.)) %>% imappend("x") %>% plot
imrotate(plant,30) %>% plot
grayscale(plant) %>% plot
msk <- px.flood(plant,400,900,sigma=.28) %>% as.cimg
inv<- msk %>%as.data.frame
inv$value<- as.numeric(inv$value==0)
msk<- inv %>% as.cimg()
## Warning in as.cimg.data.frame(.): Guessing image dimensions from maximum
## coordinate values
plot(msk*plant)
subsetImage<- function(test,xrange,yrange){
newtest<- test %>% as.data.frame
dfnew<-subset(newtest, x%in%xrange & y%in%yrange)
dfnew<- as.cimg(dfnew)
return(dfnew)}
indicatorMatrix <-function( test,xrange,yrange,types)
{
if(types=='RBG'){
newtest<- test %>% as.data.frame} else{newtest<-RGBtoHSV(test) %>% as.data.frame}
dfnew<-subset(newtest, x%in%xrange & y%in%yrange) %>% ddply( .(cc),summarize,SD = sd(value, na.rm=TRUE),Mean = mean(value, na.rm=TRUE))
tmp<-subset(newtest,cc==1)
tmp$value<-as.numeric( between (newtest$value[newtest$cc==1] , unlist(dfnew[1,3]-dfnew[1,2]), unlist(dfnew[1,3]+dfnew[1,2]) ) & between ( newtest$value[newtest$cc==2 ] , unlist(dfnew[2,3]-dfnew[2,2]), unlist(dfnew[2,3]+dfnew[2,2]))
& between ( newtest$value[newtest$cc==3] , unlist(dfnew[3,3]-dfnew[3,2]), unlist(dfnew[3,3]+dfnew[3,2])))
newtest$value<- rep(tmp$value,3) *newtest$value
tester<- newtest %>% as.cimg
return(list(tester,tmp,dfnew))
}
samplemat3<- indicatorMatrix(plant,900:1000 ,850:950,'RBG')
## Warning in as.cimg.data.frame(.): Guessing image dimensions from maximum
## coordinate values
#Isolate Leaves
plot(samplemat3[[1]])
#Correlate2D<-function ( A, B ){
# a = ft.fft2(A)
## c = a %*% b
# C = ft.ifft2( c )
# C = ft.fftshift(C)
# return C
#}
#bw <- plant %>% grayscale
#ff <- FFT(bw)
#a=ff %>% as.data.frame
#plot(ff$real,main="Real part of the transform")
samplemathsv<- indicatorMatrix(plant,900:1000 ,850:950,'HSV')
## Warning in as.cimg.data.frame(.): Guessing image dimensions from maximum
## coordinate values
#Isolate Leaves
plot(samplemathsv[[1]])
#Correlate2D<-function ( A, B ){
# a = ft.fft2(A)
## c = a %*% b
# C = ft.ifft2( c )
# C = ft.fftshift(C)
# return C
#}
#bw <- plant %>% grayscale
#ff <- FFT(bw)
#a=ff %>% as.data.frame
#plot(ff$real,main="Real part of the transform")
gray<- grayscale(samplemat3[[1]] )
grayshrink<- shrink(gray,8)
#Isolate Leaves
plot(grayshrink)
#Correlate2D<-function ( A, B ){
# a = ft.fft2(A)
## c = a %*% b
# C = ft.ifft2( c )
# C = ft.fftshift(C)
# return C
#}
#bw <- plant %>% grayscale
#ff <- FFT(bw)
#a=ff %>% as.data.frame
#plot(ff$real,main="Real part of the transform")
gray<- grayscale(samplemat3[[1]] )
graygrow<- grow(gray,2)
#Isolate Leaves
plot(graygrow)
#Correlate2D<-function ( A, B ){
# a = ft.fft2(A)
## c = a %*% b
# C = ft.ifft2( c )
# C = ft.fftshift(C)
# return C
#}
#bw <- plant %>% grayscale
#ff <- FFT(bw)
#a=ff %>% as.data.frame
#plot(ff$real,main="Real part of the transform")
gray<- grayscale(plant )
pcamat<- gray %>% as.data.frame %>% data.table
pcamat2<- data.table::dcast(pcamat,x~y, value.var='value') %>% as.matrix
photo.pca <- prcomp(pcamat, center = F)
#PCs <- c(round(0.9 * nrow(pcamat2)))
photo<- pcamat
photo[,3]<- pcamat[,3]*photo.pca$x[3,1]
photo%>% as.cimg%>% plot
## Warning in as.cimg.data.frame(.): Guessing image dimensions from maximum
## coordinate values
#Isolate Leaves
Area<- sum(samplemat3[[2]]$value)
print(Area)
## [1] 102595
binImageShrink <- samplemat3[[2]] %>% as.cimg %>% shrink(1) %>% as.data.frame
## Warning in as.cimg.data.frame(.): Guessing image dimensions from maximum
## coordinate values
Perimeter<- sum(samplemat3[[2]]$value)- sum(binImageShrink$value)# ) %>% as.cimg %>%
print(Perimeter)
## [1] 102595
binImageShrink <- samplemat3[[2]] %>% as.cimg %>% shrink(1) %>% as.data.frame
## Warning in as.cimg.data.frame(.): Guessing image dimensions from maximum
## coordinate values
sum(samplemat3[[2]]$value)- sum(binImageShrink$cc)# ) %>% as.cimg %>% plot
## [1] 0
#Isolate Leaves
subsetDF<- subset (samplemat3[[2]], value==1)
mean(subsetDF$x)
## [1] 776.762
mean(subsetDF$y)
## [1] 872.7603
#Isolate Leaves
#Use cannyedges
leafimage<-subim(plant, x%inr% c(450,600),y%inr% c(1175,1500))
edges<-cannyEdges(leafimage)
## Warning in cannyEdges(leafimage): Running Canny detector on luminance
## channel
plot(edges)
#Isolate Leaves
#Use cannyedges
library('EBImage')
##
## Attaching package: 'EBImage'
## The following object is masked from 'package:data.table':
##
## transpose
## The following objects are masked from 'package:imager':
##
## channel, dilate, display, erode, resize, watershed
img<-readImage('~/Documents/Github/PhD//imageprocessing/burseragrav1.jpg')
grayimage<-channel(img,"gray")
crop<-grayimage[450:600,1175:1500]
y = gblur(crop, 3) < .8
y <- bwlabel(y)
contours = ocontour(bwlabel(y))
c = localCurvature(x=contours[[1]], h=11)
i = c$curvature >= 0
pos = neg = array(0, dim(crop))
pos[c$contour[i,]+1] = c$curvature[i]
neg[c$contour[!i,]+1] = -c$curvature[!i]
display(10*(rgbImage(pos, neg)), title = "Image curvature")
#Isolate Leaves
#Use cannyedges
kern = makeBrush(3)
blah=as.Image(neg)
blah2<- blah>0.0
blah3 <-dilate(blah2,kern)
logo_label = bwlabel(blah3)
table(logo_label)
## logo_label
## 0 1 2 3 4 5 6 7 8 9 10 11
## 47999 67 40 58 70 60 44 42 40 47 39 57
## 12 13 14 15 16 17 18 19 20 21 22 23
## 37 32 33 36 17 40 33 40 31 34 25 42
## 24 25 26 27
## 39 35 23 166
plot( colorLabels(logo_label) )
#Isolate Leaves
#Use cannyedges
kern = makeBrush(3)
blah=as.Image(pos)
blah2<- blah>0.0
blah3 <-dilate(blah2,kern)
logo_label = bwlabel(blah3)
display( colorLabels(logo_label) )
computeFeatures.shape(logo_label)
## s.area s.perimeter s.radius.mean s.radius.sd s.radius.min s.radius.max
## 1 43 24 3.773218 1.5581258 1.2754357 6.275089
## 2 86 42 6.713684 3.4647761 1.0546302 12.373853
## 3 53 28 4.512232 2.0255300 1.1025249 7.521483
## 4 39 20 3.633990 1.4121332 1.4142136 5.656854
## 5 46 26 4.024420 1.6647811 1.0161419 6.733516
## 6 50 30 4.101688 1.9389399 0.5000000 7.158911
## 7 36 22 3.288319 1.2468863 0.9979317 5.333893
## 8 18 14 1.960969 0.6360817 1.1180340 2.692582
## 9 37 24 3.196330 1.3599440 0.3173239 5.308863
## 10 43 24 3.443521 1.3022986 0.2667968 5.743505
## 11 31 20 2.900823 1.0932639 1.0440307 4.614109
## 12 39 24 3.409130 1.3441163 0.7276770 5.644719
## 13 33 20 2.866238 0.9392434 0.7433034 4.225222
## 14 18 14 1.960969 0.6360817 1.1180340 2.692582
## 15 36 22 3.174634 1.2248323 0.4895604 5.149019
## 16 38 22 3.183906 1.2844982 0.5081973 4.888213
## 17 37 24 3.264283 1.3670262 0.2429563 5.241726
## 18 37 24 3.270719 1.2938023 0.9807169 5.208500
## 19 18 14 1.960969 0.6360817 1.1180340 2.692582
## 20 36 22 3.254272 1.3050304 1.0775700 5.383246
## 21 34 22 3.068791 1.1308415 0.9927423 4.756249
## 22 42 26 3.662523 1.6453649 0.5131409 6.321046
## 23 47 28 4.002228 1.8440547 0.7142857 7.056622
## 24 42 26 3.665268 1.6192662 1.1525634 6.283490
## 25 42 26 3.665268 1.6192662 1.1525634 6.283490
## 26 52 34 4.480742 2.2283033 0.9634317 7.809419
## 27 59 40 5.157951 2.6823341 0.7250000 9.163822
#Isolate Leaves
#Use cannyedges
fHigh <- matrix(1, nc = 3, nr = 3)
fHigh[2, 2] <- -8
Image.fHigh <- filter2(logo_label, fHigh)
display(Image.fHigh)